/**
 * Project Name:DateTimeUtils
 * File Name:DateTimeUtils.java
 * Package Name:com.bwda.somp.common.util
 * Date:2019年4月28日
 * Copyright (c) 2019, 江苏保旺达软件有限公司 All Rights Reserved.
 */
package com.bwda.dsrs.filecenter.util;

import com.bwda.dsrs.base.util.StringUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author zhuyongxuan
 */
public class DateTimeUtils extends DateUtils {
    public static final String YYYY = "yyyy";

    public static final String YYYY_MM = "yyyy-MM";

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    public static final int MONTH_ONE = 1;
    public static final int MONTH_TWO = 2;
    public static final int MONTH_THREE = 3;
    public static final int MONTH_FOUR = 4;
    public static final int MONTH_FIVE = 5;
    public static final int MONTH_SIX = 6;
    public static final int MONTH_SEVEN = 7;
    public static final int MONTH_EIGHT = 8;
    public static final int MONTH_NINE = 9;
    public static final int MONTH_TEN = 10;
    public static final int MONTH_ELEVEN = 11;
    public static final int MONTH_TWELVE = 12;

    public static final int QUARTER_ONE = 1;
    public static final int QUARTER_TWO = 2;
    public static final int QUARTER_THREE = 3;
    public static final int QUARTER_FOUR = 4;

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getDateTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        String result = "";
        if (date == null || StringUtil.isEmpty(format)) {
            return result;
        }

        try {
            result = new SimpleDateFormat(format).format(date);
        } catch (Exception e) {
        }

        return result;
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 时间戳转化为Date
     */
    public static Date getDateByStamp(Calendar timstamp) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String d = format.format(timstamp.getTimeInMillis());
            Date date = format.parse(d);
            System.out.println("Format To Date:" + date);
            return date;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static int getMonth(int hourOffset) {
        Calendar calendar = Calendar.getInstance();
        //偏移小时数
        calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + hourOffset);
        return calendar.get(Calendar.MONTH) + 1;
    }

    public static int getDay(int hourOffset) {
        Calendar calendar = Calendar.getInstance();
        //偏移小时数
        calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + hourOffset);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static int getHour(int hourOffset) {
        Calendar calendar = Calendar.getInstance();
        //偏移小时数
        calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + hourOffset);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public static int getWeek(int hourOffset) {
        Calendar calendar = Calendar.getInstance();
        //偏移小时数
        calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + hourOffset);
        return calendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    /**
     * 根据月份获取季节
     *
     * @param month 月份
     * @return int
     */
    private static int getQuarterByMonth(int month) {
        int quarter = QUARTER_ONE;
        if (month >= MONTH_ONE && month <= MONTH_THREE) {
            quarter = QUARTER_ONE;
        } else if (month >= MONTH_FOUR && month <= MONTH_SIX) {
            quarter = QUARTER_TWO;
        } else if (month >= MONTH_SEVEN && month <= MONTH_NINE) {
            quarter = QUARTER_THREE;
        } else if (month >= MONTH_TEN && month <= MONTH_TWELVE) {
            quarter = QUARTER_FOUR;
        }
        return quarter;
    }

    /**
     * 获取季度
     *
     * @param hourOffset
     * @return
     */
    public static int getQuarter(int hourOffset) {
        Calendar calendar = Calendar.getInstance();
        //偏移小时数
        calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + hourOffset);
        int month = calendar.get(Calendar.MONTH) + 1;

        int quarter = getQuarterByMonth(month);
        return quarter;
    }

    /**
     * 获取当前时间是本季度的第几天
     * @return
     */
    public static int getQuarterDay() {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        //季度第一天
        Date firstDay = null;
        //得到相差的天数
        int betweenDate = 0;
        try {
            //当前时间
            Date now = sdf.parse(sdf.format(c.getTime()));
            if (currentMonth >= MONTH_ONE && currentMonth <= MONTH_THREE) {
                c.set(Calendar.MONTH, 0);
            } else if (currentMonth >= MONTH_FOUR && currentMonth <= MONTH_SIX) {
                c.set(Calendar.MONTH, MONTH_THREE);
            } else if (currentMonth >= MONTH_SEVEN && currentMonth <= MONTH_NINE) {
                c.set(Calendar.MONTH, MONTH_SIX);
            } else if (currentMonth >= MONTH_TEN && currentMonth <= MONTH_TWELVE) {
                c.set(Calendar.MONTH, MONTH_NINE);
            }
            c.set(Calendar.DATE, 1);
            firstDay = sdf.parse(sdf.format(c.getTime()));
            betweenDate = (int) ((now.getTime() - firstDay.getTime()) / (60 * 60 * 24 * 1000)) + 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return betweenDate;
    }


    public static int getBetweenDay(Date createTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        //相差的天数
        int betweenDate = 0;
        Date now = new Date();
        try {
             now = sdf.parse(sdf.format(now));
             createTime = sdf.parse(sdf.format(createTime));
             betweenDate = (int) ((now.getTime() - createTime.getTime()) / (60 * 60 * 24 * 1000));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return betweenDate;
    }

    /**
     * 比较两个时间大小
     * <p>
     * 0: dateTime1 = dateTime2
     * 1: dateTime1 > dateTime2
     * 2: dateTime1 < dateTime2
     * 9: 异常
     *
     * @return int
     */
    public static int compareDateTime(String dateTime1, String dateTime2) {
        int result = 0;
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        try {
            long d1 = sdf.parse(dateTime1).getTime();
            long d2 = sdf.parse(dateTime2).getTime();
            if (d1 == d2) {
                result = 0;
            } else if (d1 > d2) {
                result = 1;
            } else if (d1 < d2) {
                result = 2;
            }
        } catch (Exception e) {
            result = 9;
        }

        return result;
    }

    /**
     * 比较两个日期大小
     * <p>
     * 0: date1 = date2
     * 1: date1 > date2
     * 2: date1 < date2
     * 9: 异常
     *
     * @return int
     */
    public static int compareDate(String date1, String date2) {
        int result = 0;
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        try {
            long d1 = sdf.parse(date1).getTime();
            long d2 = sdf.parse(date2).getTime();
            if (d1 == d2) {
                result = 0;
            } else if (d1 > d2) {
                result = 1;
            } else if (d1 < d2) {
                result = 2;
            }
        } catch (Exception e) {
            result = 9;
        }

        return result;
    }

    /**
     * 计算两个时间的间隔
     *
     * @param beginDateTime
     * @param endDateTime
     * @return long
     */
    public static long minusDate(Date beginDateTime, Date endDateTime) {
        long result = 0;
        try {
            long d1 = beginDateTime.getTime();
            long d2 = endDateTime.getTime();
            result = d2 - d1;
        } catch (Exception e) {
        }

        return result;
    }

    public static void main(String[] args) {


    }
}
